#include <stdlib.h>
#include "includes/game.h"
#include "includes/menu_state.h"
#include "includes/see_state.h"

Game * game(){
	static Game game;
	return &game;
}

State * game_state(){
	return game()->current_state;
}

GLvoid game_init(){
	
	State * ms = &game()->menu_state;
	State * ss = &game()->see_state;
	
	/* Mise en place l'etat "menu" */
	ms->init_handler 	= menu_state_init;
	ms->main_handler 	= menu_state_main;
	ms->event_key 		= menu_state_event_keyboard;
	ms->event_special	= menu_state_event_special;
	ms->resize 			= menu_state_resize;
	ms->env 			= malloc(sizeof(Menu_State_Env));
	
	/* Mise en place de l'etat "voir" */
	ss->init_handler	= see_state_init;
	ss->main_handler	= see_state_main;
	ss->event_key		= see_state_event_keyboard;
	ss->event_special	= see_state_event_special;
	ss->resize			= see_state_resize;
	ss->env				= malloc(sizeof(See_State_Env));
	
	game()->current_state = ms;
}

GLvoid game_display(){
	game_state()->main_handler();
}

GLvoid game_resize(GLsizei width, GLsizei height){  
	game_state()->resize(width, height);
}

GLvoid game_event_keyboard(unsigned char key, int x, int y){
	game_state()->event_key(key, x, y);
}

GLvoid game_event_special(int key, int x, int y){
	game_state()->event_special(key, x, y);
}

GLvoid game_exit(){
	free(ms_env());
	free(ss_env());
	exit(0);
}